/**
 *
 * @param date
 * @param fmt
 * @returns {*}
 * @author wan
 * @description 用于将时间戳转化为时间格式，使用时在vue中调用该js方法
 */
export default function formatDate(date: Date, fmt: string) {
  if (/(y+)/.test(fmt)) {
    fmt = fmt.replace(
      RegExp.$1,
      (date.getFullYear() + "").substr(4 - RegExp.$1.length)
    );
  }
  const dayList = ["日", "一", "二", "三", "四", "五", "六"];
  const o = {
    "M+": date.getMonth() + 1,
    "d+": date.getDate(),
    "h+": date.getHours(),
    "m+": date.getMinutes(),
    "s+": date.getSeconds(),
    "w+": dayList[date.getDay()],
  };
  for (const k in o) {
    if (new RegExp(`(${k})`).test(fmt)) {
      const str = o[k] + "";
      fmt = fmt.replace(
        RegExp.$1,
        RegExp.$1.length === 1 ? str : padLeftZero(str)
      );
    }
  }
  return fmt;
}

function padLeftZero(str: string) {
  return ("00" + str).substr(str.length);
}
